package com.seal.commons.infrastructure.facade.jms;

import com.seal.commons.utils.serializer.ObjectSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.ParameterizedType;

/**
 * ClassName:AbstractMessageListener <br/>
 * Function:  进行反序列化操作 <br/>
 * Date:     2019-12-19 23:25 <br/>
 *
 * @author yong.huang
 * @see
 * @since JDK 1.8
 */
public abstract class AbstractMessageListener<T> implements MessageListener {

    final static Logger LOGGER = LoggerFactory.getLogger(AbstractMessageListener.class);

    @Override
    public void onMessage(String msgId, String tags, byte[] bytes){
       T t = (T) ObjectSerializer.deserialize(bytes, getTClass() );
       LOGGER.info("topic:[{}], tags:[{}], body:[{}]", msgId, tags, t);
       onMessage(msgId, tags, t);
    }

    public Class<T> getTClass() {
        return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }


    protected abstract <T> T onMessage(String msgId, String tags, T t);
}
